package com.xxkk.net.websocketNetty;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;

import static org.jboss.netty.channel.Channels.pipeline;

/**
 * @author xxkk
 * @version 1.0.0
 * @title WebSocketServerPipelineFactory
 * @description ʕ•ᴥ•ʔ
 * @create 2023/11/13 15:38
 **/
public class WebSocketServerPipelineFactory implements ChannelPipelineFactory {
    private ChannelUpstreamHandler upstreamHandler;

    public WebSocketServerPipelineFactory(ChannelUpstreamHandler upstreamHandler) {
        this.upstreamHandler = upstreamHandler;
    }

    @Override
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = pipeline();
        pipeline.addLast("decoder", new HttpRequestDecoder());
        pipeline.addLast("encoder", new HttpResponseEncoder());
        pipeline.addLast("webHandler", new WebSocketServerProtocolHandler("/xxkk"));
        pipeline.addLast("handler", upstreamHandler);
        return pipeline;
    }
}
